package com.cskaoyan.controller;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.param.User;
import com.cskaoyan.exception.MarketValidationException;
import com.cskaoyan.service.UserService;
import com.cskaoyan.util.ValidationUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author stone
 * @date 2022/06/25 11:05
 */
@RestController
@RequestMapping("admin/user")
public class AdminUserController {

    @Autowired
    UserService userService;

    /*@RequestMapping("list")
    public BaseRespVo list(Integer page,Integer limit, String sort,String order) {

    }*/

    // 将Handler方法和权限建立耦合关系，访问这个Handler方法需要注解中的权限
    //@RequiresPermissions("aaa")
    /*@RequestMapping("list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo list(String username, @Validated BasePageInfo info, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            String field = fieldError.getField();//请求参数名
            Object rejectedValue = fieldError.getRejectedValue();//错误的值
            String defaultMessage = fieldError.getDefaultMessage(); //错误的消息
            String msg = "请求参数" + field + "没有通过校验：值为" + rejectedValue + ";" + defaultMessage;
            return BaseRespVo.invalidParameter(msg);
        }

        CommonData<User> data =  userService.query(username,info);
        return BaseRespVo.ok(data);
    }*/

    /*@RequestMapping("list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo list(String username, @Validated BasePageInfo info, BindingResult bindingResult) {
        if (bindingResult.hasFieldErrors()) {
            return ValidationUtils.getBaseRespVo(bindingResult);
        }
        CommonData<User> data =  userService.query(username,info);
        return BaseRespVo.ok(data);
    }*/

    @RequestMapping("list")
    //public BaseRespVo list(String username, Integer page, Integer limit, String sort, String order) {
    public BaseRespVo list(String username, @Validated BasePageInfo info, BindingResult bindingResult) throws MarketValidationException {
        ValidationUtils.valid(bindingResult);
        CommonData<User> data =  userService.query(username,info);
        return BaseRespVo.ok(data);
    }


}
